﻿using System;
using System.Collections.Generic;
using BuzzX8.Xmpp.StreamElements.Stanza;

namespace BuzzX8.Xmpp.StreamElements.Builders
{
    public class MessageStanzaBuilder : StanzaBuilder<MessageStanzaType, MessageStanza>
    {
        protected override MessageStanzaType GetStanzaTypeForAttributeValue(string value)
        {
            switch (value)
            {
                case "":
                    return MessageStanzaType.None;

                case "chat":
                    return MessageStanzaType.Chat;

                case "error":
                    return MessageStanzaType.Error;

                case "groupchat":
                    return MessageStanzaType.GroupChat;

                case "headline":
                    return MessageStanzaType.Headline;

                case "normal":
                    return MessageStanzaType.Normal;

                default:
                    throw new NotImplementedException();
            }
        }

        protected override IEnumerable<string> GetSupportedElementNames()
        {
            return new [] {"message"};
        }

        protected override MessageStanza CreateNewElement(string name)
        {
            if (name == "message") return new MessageStanza();
            throw new NotImplementedException();
        }

        protected override void SetEmbeddedNodeValue(string nodeName, string value)
        {
            switch (nodeName)
            {
                case "body":
                    RootElement.Body = value;
                    break;

                case "subject":
                    RootElement.Subject = value;
                    break;

                case "thread":
                    RootElement.ThreadId = value;
                    break;
            }
        }

        protected override void AddAttributeToEmbeddedNode(string name, string value)
        {
            if (name == "parent" && CurrentNodeName == "thread") RootElement.ParentThreadId = value;
        }
    }
}
